package com.learning.platform.service;

import com.learning.platform.entity.Progress;
import com.learning.platform.repository.ProgressRepository;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service
@Transactional
public class ProgressService {
    
    private final ProgressRepository progressRepository;
    
    public ProgressService(ProgressRepository progressRepository) {
        this.progressRepository = progressRepository;
    }
    
    public void updateProgress(Long userId, Long lessonId, Integer progressPercent) {
        Progress progress = progressRepository.findByUserIdAndLessonId(userId, lessonId)
                .orElse(new Progress(userId, lessonId, 0));
        
        progress.setProgressPercent(Math.max(0, Math.min(100, progressPercent)));
        progressRepository.save(progress);
    }
    
    @Transactional(readOnly = true)
    public Integer getProgress(Long userId, Long lessonId) {
        return progressRepository.findByUserIdAndLessonId(userId, lessonId)
                .map(Progress::getProgressPercent)
                .orElse(0);
    }
}










